var users = {
		getUsers : function(id){
			var eventid = sessionStorage.getItem("eventid");
			var usersCache = sessionStorage.getItem("usersCache" + eventid);
			if(usersCache == null){
				$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=eventusers,eventid=" + eventid +"&address=http://103.37.166.4:7777/StateGrid/ua.do",
					scriptCharset: 'utf-8', 
					success : function(param) {
						param = JSON.parse(param);
						if(param.stat == "1"){
							sessionStorage.setItem("usersCache" + eventid,JSON.stringify(param.data));
							users.list = param.data;
							users.createUserList(param.data,id);
						}
					},
					error: function(){
					}
				});
			}else{
				usersCache = JSON.parse(usersCache);
				users.list = usersCache;
				users.createUserList(usersCache,id);
			}
		},
		createUserList : function(userList,id){
			var str = "";
			if(id == "#userList_q"){
				for(var i=0;i<userList.length;i++){
					if(userList[i].realizeUsers.length >0){
						str += '<li><input type="checkbox" id="'+userList[i].ecdrepId+'jsid" onclick="checkSingle(this)"><span  onclick="users.selectUser(this,\''+userList[i].ecdrepId+'\',\''+id+'\')">'+userList[i].ecdrepName+'</span><ul>';
						for(var j=0;j<userList[i].realizeUsers.length;j++){
							str += '<li><input type="checkbox" id="'+userList[i].realizeUsers[j].su_id+'yhid'+userList[i].ecdrepId+'" onclick="checkSingle(this)"><a href="javascript:;">'+userList[i].realizeUsers[j].su_name+'</a></li>';
						}
						str += '</ul></li>';
					}
				}
			}else{
				users.selectedUser = "ALL";
				str  = '<li class="current"  onclick="users.selectUser(this,\'ALL\')" id="allUsers"><span>所有人</span><ul></ul></li>';
				for(var i=0;i<userList.length;i++){
					if(userList[i].realizeUsers.length >0){
						str += '<li  onclick="users.selectUser(this,\''+i+'\')"><span>'+userList[i].ecdrepName+'</span><ul>';
						for(var j=0;j<userList[i].realizeUsers.length;j++){
							str += '<li><a href="javascript:;">'+userList[i].realizeUsers[j].su_name+'</a></li>';
						}
						str += '</ul></li>';
					}
				}
			}
			$(id).html(str);
			$(".current").css("background","#fff9e8");
		},
		selectUser : function(u,i,id){
			$(".current").css("background","");
			if(id == "#userList_q"){
				$(u).parent().toggleClass('current').siblings().removeClass('current');
			}else{
				$(u).toggleClass('current').siblings().removeClass('current');
			}
			if($(u).hasClass('current')){
				users.selectedUser = users.list[i];
			}else{
				$("#allUsers").addClass('current').siblings().removeClass('current');
				users.selectedUser = "ALL";
			}
			console.info(users.selectedUser);
			$(".current").css("background","#fff9e8");
		},
		selectedUser : "",
		checkedUser : "",
		list : ""
}

var communication = {
		openWindow : function(){
			users.getUsers("#userList_c");
			communication.getRecord();
		},
		send : function(){
			var eventid = eventids;
			var crid = sessionStorage.getItem("crid");
			var text = $("#content_c").val();
			var filename = "";
			var receiveData;
			var sesuid = JSON.parse(sessionStorage.getItem("suid")).data.su_id;
			var sesuname = JSON.parse(sessionStorage.getItem("suid")).data.su_name;
			var sepid = "";;
			var sepname = "";
			for(var i in users.list){
				if(users.list[i].realizeUsers != null && users.list[i].realizeUsers != ""){
					for(var j in users.list[i].realizeUsers){
						if(users.list[i].realizeUsers[j].su_id == sesuid){
							sepid = users.list[i].ecdrepId;
							sepname = users.list[i].ecdrepName;
						}
					}
				}
			}
			if(users.selectedUser == "ALL"){
				receiveData = ",resuid=allofus,resuname=allofus,repid=allofus,repname=allofus";
			}else{
				var resuid = users.selectedUser.realizeUsers[0].su_id;
				var resuname = users.selectedUser.realizeUsers[0].su_name;
				var repid = users.selectedUser.ecdrepId;
				var repname = users.selectedUser.ecdrepName;
//				var ALL ="allofus"
				receiveData = ",resuid=" + resuid +",resuname=" + resuname +",repid=" + repid +",repname=" + repname;
			}
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=insert,crid=" + crid +",eventid=" + eventid +",text=" + text +",filename=" + filename +",sesuid=" + sesuid +",sesuname=" + sesuname +",sepid=" + sepid +",sepname=" + sepname + receiveData +"&address=http://103.37.166.4:7777/StateGrid/cha.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
					console.info(param);
					param = JSON.parse(param);
					if(param.stat == "1"){
						$("#content_c").val("");
						if(param.data.recvUserName == "allofus"){
							param.data.recvUserName = "所有人";
						}
						var str = '<p class="red">我对'+param.data.recvUserName+'说：<span  style="color: #333">'+param.data.content+'</span></p>';
						$(str).appendTo("#messAll");
					}
				},
				error: function(){
				}
			});
		},
		getRecord : function(){
			var suid = JSON.parse(sessionStorage.getItem("suid")).data.su_id;
			var eventid = eventids;
			var crid = sessionStorage.getItem("crid");
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getuchat,crid=" + crid +",eventid=" + eventid +",suid=" + suid +"&address=http://103.37.166.4:7777/StateGrid/cha.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
					console.info(param);
					param = JSON.parse(param);
					if(param.stat == "1"){
						console.info(param);
						//append聊天记录
						communication.createRecord(param.data);
					}
				},
				error: function(){
				}
			});
		},
		createRecord : function(record){
			var suid = JSON.parse(sessionStorage.getItem("suid")).data.su_id;
			var str = ""
			for(var i in record){
				if(record[i].sendSuId == suid){
					str += '<p class="red">';
					record[i].sendUserName = "我";
				}else{
					str += '<p class="">';
				}
				if(record[i].recvUserName == "allofus" ){
					record[i].recvUserName = "所有人";
				}
				if(record[i].recvSuId == suid ){
					record[i].recvUserName = "我";
				}
				str += record[i].sendUserName + '对'+ record[i].recvUserName +'说：<span style="color: #333">'+ record[i].content +'</span></p>';
			}
			$("#messAll").html(str);
		}
		
}
$(function() {
	$("#message").click(function() {
		if($(this).hasClass('active')) {
			communication.openWindow();
		}
	});
	$("#send_c").click(function(){
		communication.send();
	});
});
